<!--
 * @Description: 价格明细
 * 
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-04-23 22:52:27
 * @FilePath: /yuxiuproweb/src/views/mes/pro/workorder/mods/add/detail.vue
-->
<template>
  <el-dialog
    :close-on-click-modal="false"
    v-dialogDrag
    title="价格明细"
    :visible.sync="shows"
    width="1160px"
    append-to-body
  >
    <div class="detail">
      <div class="detail-left">
        <el-table ref="multipleTable" :data="modelFabricList" :height="175">
          <el-table-column width="60" align="center">
            <template slot-scope="{ row }">
              <el-radio
                v-model="row.checked"
                class="radio-view"
                :label="true"
                @change="checkedModelChange(row)"
                ><i></i
              ></el-radio>
            </template>
          </el-table-column>
          <el-table-column label="位置" prop="part">
            <template slot-scope="{ row }">
              <template v-if="row.type === '0'">
                {{
                  getTypeName(
                    dict.type.md_complex_machining_model_part_1,
                    row.part
                  )
                }}
              </template>
              <template v-else-if="row.type === '1'">
                {{
                  getTypeName(
                    dict.type.md_complex_machining_model_part_2,
                    row.part
                  )
                }}
              </template>
              <template v-else-if="row.type === '-1'">主布</template>
            </template>
          </el-table-column>
          <el-table-column label="来源" prop="source">
            <template slot-scope="{ row }">
              <el-select
                v-model="row.source"
                clearable
                placeholder="请选择"
                style="width: 100%"
                size="mini"
              >
                <el-option label="自有" value="0"> </el-option>
                <el-option label="余料" value="1"> </el-option>
                <el-option label="来料" value="2"> </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="品名" prop="fabricName"></el-table-column>
          <el-table-column label="门幅" prop="fabricWidth"></el-table-column>
          <el-table-column label="方向" prop="direction">
            <template slot-scope="{ row }" v-if="row.type !== '1'">
              <el-select
                v-model="row.direction"
                clearable
                placeholder="请选择"
                style="width: 100%"
                size="mini"
                @change="handleDirectionChange(row.direction)"
              >
                <el-option label="定宽" value="0"> </el-option>
                <el-option label="定高" value="1"> </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="number">
            <template slot-scope="{ row }">
              <el-input
                style="width: 100%"
                v-model="row.number"
                type="text"
                placeholder="请输入"
                size="mini"
              />
            </template>
          </el-table-column>
          <el-table-column label="单位" prop="unit">
            <template>米</template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="part mt20">
      <div class="part-left">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          @submit.native.prevent
        >
          <el-form-item label="品名" prop="name">
            <el-input
              v-model.trim="queryParams.name"
              placeholder="请输入品名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <template v-if="modelFabricRow && modelFabricRow.type !== '1'">
          <el-table ref="multipleTable" :data="fabricList" :max-height="370">
            <el-table-column width="60" align="center">
              <template slot-scope="{ row }">
                <div @click="checkedChange(row)" style="cursor: pointer">
                  <i class="el-icon-upload2"></i>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="品名" prop="name">
              <template slot-scope="{ row }">
                <el-link
                  type="primary"
                  @click="setImage(row), checkedChange(row)"
                  >{{ row.name }}</el-link
                >
              </template>
            </el-table-column>
            <el-table-column
              label="相关信息"
              prop="salesAlias"
            ></el-table-column>
            <el-table-column
              label="价格"
              prop="wholesalePrice1"
              width="80"
            ></el-table-column>
          </el-table>
        </template>
        <template v-else-if="modelFabricRow && modelFabricRow.type === '1'">
          <el-table ref="multipleTable" :data="materialList" :max-height="370">
            <el-table-column width="60" align="center">
              <template slot-scope="{ row }">
                <div @click="checkedChange(row)" style="cursor: pointer">
                  <i class="el-icon-upload2"></i>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="品名" prop="name">
              <template slot-scope="{ row }">
                <el-link
                  type="primary"
                  @click="setImage(row), checkedChange(row)"
                  >{{ row.name }}</el-link
                >
              </template>
            </el-table-column>
            <el-table-column
              label="相关信息"
              prop="salesAlias"
            ></el-table-column>
            <el-table-column
              label="价格"
              prop="wholesalePrice"
              width="80"
            ></el-table-column>
          </el-table>
        </template>
      </div>
      <div class="part-right ml10">
        <el-image :src="skuImg">
          <div slot="error" class="image-slot">
            <i class="el-icon-picture-outline"></i>
          </div>
        </el-image>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="shows = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listFabric } from "@/api/material/fabric";
import { listMaterial } from "@/api/material/other";
import { TYPE } from "@/views/material/fabric/configure";
import { listFabric as listModelFabric } from "@/api/process/model";
export default {
  dicts: [
    "md_complex_machining_model_part_1",
    "md_complex_machining_model_part_2",
  ],
  props: {
    show: Boolean,
    data: {
      type: Object,
    },
  },
  data() {
    return {
      TYPE,
      fabricList: [],
      materialList: [],
      list: [],
      multipleSelection: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        name: null,
      },
      // 总条数
      total: 0,
      skuImg: "",
      checkedRow: null,
      modelFabricList: [],
      modelFabricRow: null,
      timer: null,
    };
  },
  watch: {
    "queryParams.name": {
      handler() {
        if (this.timer) clearTimeout(this.timer);
        this.timer = setTimeout(() => {
          this.handleQuery();
        }, 500);
      },
    },
  },
  computed: {
    shows: {
      get() {
        return this.show;
      },
      set(newValue) {
        this.$emit("update:show", newValue);
      },
    },
  },
  created() {
    console.log(this.data);
    this.getList();
    this.getListMaterial();
    this.getModelFabricList();
  },
  methods: {
    /** 物料 */
    getModelFabricList() {
      listModelFabric({
        pageNum: 1,
        pageSize: 100,
        complexMachiningModelId: this.data.complexMachiningModelId,
      }).then(({ rows }) => {
        this.modelFabricList = [
          {
            type: "-1",
            part: "-1",
            source: "0",
            direction: "0",
            number: 1,
            checked: true,
          },
          ...rows?.map((e) => {
            return {
              ...e,
              source: "0",
              direction: "0",
              number: 1,
              checked: false,
            };
          }),
        ];
        this.modelFabricRow = this.modelFabricList?.find((e) => e.checked);
      });
    },
    /** 查询面料维护列表 */
    getList() {
      listFabric(this.queryParams).then(({ rows, total }) => {
        this.fabricList = rows || [];
      });
    },
    /** 查询辅料列表 */
    getListMaterial() {
      listMaterial(this.queryParams).then(({ rows, total }) => {
        this.materialList = rows || [];
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.getListMaterial();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
    /** 显示右侧图片 */
    setImage({ skuPicture }) {
      this.skuImg = skuPicture;
    },
    checkedChange(row) {
      this.checkedRow = {
        ...row,
      };
      const cellIndex = this.modelFabricList?.findIndex((e) => e.checked),
        cell = this.modelFabricList[cellIndex];
      cell.fabricName = row.name;
      cell.fabricWidth = row?.width || "";
      if (this.modelFabricRow.type !== "1") {
        cell.fabricId = row.mdFabricId;
        cell.wholesalePrice1 = row.wholesalePrice1;
        cell.direction = row.widthOrHeight === 0 ? "1" : "0";
        this.handleDirectionChange(cell.direction);
      } else {
        cell.fabricId = row.otherMaterialId;
        cell.wholesalePrice1 = row.wholesalePrice;
      }

      this.$set(this.modelFabricList, cellIndex, cell);

      if (this.modelFabricList?.length > cellIndex + 1) {
        this.modelFabricList[cellIndex].checked = false;
        this.modelFabricList[cellIndex + 1].checked = true;
      }
      this.modelFabricRow = this.modelFabricList?.find((e) => e.checked);
    },
    handleDirectionChange(direction) {
      this.modelFabricList = this.modelFabricList?.map((e) => {
        return {
          ...e,
          direction,
        };
      });
    },
    checkedModelChange(row) {
      this.modelFabricList?.forEach((e) => {
        if (
          e.mdComplexMachiningModelFabricId !==
          row.mdComplexMachiningModelFabricId
        )
          e.checked = false;
      });
      this.modelFabricRow = this.modelFabricList?.find((e) => e.checked);
    },
    /** 提交按钮 */
    async submitForm() {
      try {
        this.$emit(
          "on-change",
          this.modelFabricList.map((e) => {
            return {
              direction: e.direction,
              fabricId: e.fabricId,
              larghezza: e.fabricWidth,
              name: e.fabricName,
              number: e.number,
              part: e.part,
              source: e.source,
              part2: e.type,
              type: "0",
              unit: e.unit || "",
              wholesalePrice1: e.wholesalePrice1,
            };
          })
        );
        this.shows = false;
      } catch (e) {
        console.log(e);
      }
    },
  },
};
</script>

<style scoped lang="scss">
.detail {
  width: 100%;
  display: flex;
  overflow: hidden;
  &-left {
    flex: 1;
  }
  &-right {
    flex: 1;
  }
}
.part {
  width: 100%;
  display: flex;
  overflow: hidden;
  &-left {
    width: 700px;
  }
  &-right {
    flex: 1;
    border: 1px solid rgba(220, 220, 220, 0.5);
    border-radius: 4px;
    padding: 10px;
    img {
      width: 400px;
      height: 400px;
    }
    ::v-deep {
      .image-slot {
        width: 400px;
        height: 400px;
        font-size: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #909399;
      }
    }
  }
}
</style>
